{% extends "base.html" %}

{% block content %}
<div class="row">
  <div class="col-md-12">
    <div class="box box-widget">

      <div class="box-body">

        <form class="form-horizontal" id="update_form" method="post" action="">{% csrf_token %}
          <div class="form-group">
            <label class="col-md-1 col-md-offset-1 control-label">Username</label>
            <div class="col-md-9">
              <input type="text" class="form-control" name="user" value="{{ data.user.username }}" disabled>
            </div>
          </div>
          <div class="form-group">
            <label class="col-md-1 col-md-offset-1 control-label">PrivateKey</label>
            <div class="col-md-9">
              <textarea type="email" class="form-control" name="pkey" rows="27">{{ data.pkey|default:'' }}</textarea>
            </div>
          </div>

          <div class="form-group">
            <div class="col-md-9 col-md-offset-2">
              <button type="submit" class="btn btn-primary btn-block">SAVE</button>
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>
{% endblock %}

{% block js %}
<script src="/static/js/django.ajax.post.csrf.js"></script>
<script>
    $('#page_header').text('我的资料');
    $('#page_header_descript').text('user profile');

    // submit
    $('#update_form').submit(function (event) {
      event.preventDefault();

      if (confirm("确认提交这些数据吗？")) {
        params = $('#update_form').serializeJson();

        $.ajax({
          type: "post",
          url: "/accounts/profile/",
          data: JSON.stringify(params),
          dataType: "json",
          success: function (data) {
            if (data.state) {
              toastr.success(data.message);
            } else {
              toastr.error(data.message)
            }
          },
          error: function () {
            toastr.error('Error: ajax 请求出错！')
          }
        });
      }
    });

</script>
{% endblock %}